package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;
import model.User;

public class UserDaoImpl implements  UserDao{
	private Connection con;
    private PreparedStatement state=null;
    private ResultSet rs=null;
    private Transaction tx=null;
    private Session session=null;
    public UserDaoImpl() throws Exception{
    }
	public boolean insertUser(User user) throws Exception {
		boolean test = false;
		String name=user.getUsername();
		try{
			session=HibernateUtil.getSession();
			session.save(user);
			session.beginTransaction().commit();
				test=true;
		}catch(HibernateException e){
			test=false;
			if (tx != null) {
			    tx.rollback();
			   }
			   throw e;}
		finally{
			HibernateUtil.getSession().close();
		}
		return test;
	}

	@Override
	public boolean updateUser(User user) throws Exception {
		boolean test = false;
			try{
				session=HibernateUtil.getSession();
				session.update(user);
				session.beginTransaction().commit();
				test=true;
				}catch(HibernateException e){
				test=false;
				if (tx != null) {
				    tx.rollback();
				   }
				   throw e;
				}finally{
					if(session!=null)
					{
						session.close();
					}
			}
		return test;
	}
	public boolean deleteUser(User user) throws Exception {
		boolean test = true;
		try{	
			session=HibernateUtil.getSession();
			session.delete(user);
			session.beginTransaction().commit();
				test=true;
			}catch(HibernateException e){
				test=false;
				if (tx != null) {
				    tx.rollback();
				   }
				   throw e;
			}finally{
					HibernateUtil.getSession().close();
				}
		return test;
	}

	@Override
	public List<User> findAll() throws Exception {
		  List<User> list=null;
			  // Criteria c=session.createCriteria(User.class);
			    String hql = "from User as user";  
			  try{ 
				  session=HibernateUtil.getSession();
				  Query query = session.createQuery(hql);  
			    list = query.list(); 
			  }catch(Exception e){
					tx.rollback();
					e.printStackTrace();
			   }finally{
					HibernateUtil.getSession().close();
				}
			return list;
	  }
	public User findById(int Id) throws Exception {
		    User user = null;  
		    String hql = "from User as u where u.Id =:uId";  
		    try{ 
		    	session=HibernateUtil.getSession();
				Query query = this.session.createQuery(hql);
			   query.setParameter("uId",Id);
			   user = (User) query.list().get(0); 
				   }catch (Exception e) {
						e.printStackTrace();
					}finally{
						HibernateUtil.getSession().close();
					}
			   return user;
	  }

	@Override
	public User findByName(String username) throws Exception {
		  User user=null;
		  String hql = "from User as u where u.username =:uuserName";
		try{ 
			session=HibernateUtil.getSession();
			Query query = this.session.createQuery(hql);
		   query.setParameter("uuserName",username);
		   user = (User) query.list().get(0); 
			   }catch (Exception e) {
					e.printStackTrace();
				}finally
				{
					if(session!=null)
					{
						session.close();
					}
				}
		   return user;
	  }
	@SuppressWarnings("unchecked")
	public boolean IsName(String username) throws Exception {
		 {
			  List<User> list=null;
			   try
				{
				   session=HibernateUtil.getSession();
				   Criteria c=session.createCriteria(User.class);
				   c.add(Restrictions.eq("username",username));
				   list=c.list();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				finally
				{
					if(session!=null)
					{
						session.close();
					}
				}
				if (list.size()>0)
					return true;
				else 
					return false;
		  }
		//finally{
		//		HibernateUtil.getSession().close();
		//	}
	  }
	public void rsClose() throws Exception {
		if (rs!=null) 
			rs.close();
	}
	public void stateClose() throws Exception {
		if (state!=null) 
			state.close();
	}

}
